/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.frame;

import static com.vertexnet.realestate.constants.UIConstants.FORGOT_PASS_BUTTON;
import static com.vertexnet.realestate.constants.UIConstants.FORGOT_PASS_LABEL;
import static com.vertexnet.realestate.constants.UIConstants.LOGIN_BUTTON;

import java.awt.CardLayout;
import java.awt.Cursor;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;

import com.vertexnet.realestate.controller.LoginController;
import com.vertexnet.realestate.controller.RealEstateUtilityController;
import com.vertexnet.realestatenet.model.bean.SecurityQuestion;

/**
 *
 * @author Srivathsa
 */
public class LoginForm extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	private LoginController controller;
	
	/**
     * Creates new form LoginForm
     */
    public LoginForm() {
        initComponents();
        this.controller = new LoginController(this);
        forgotPasswordLabel.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        mainPanel = new javax.swing.JPanel();
        loginPanel = new javax.swing.JPanel();
        userIdLoginLabel = new javax.swing.JLabel();
        passwordLoginLabel = new javax.swing.JLabel();
        userIdLoginTextField = new javax.swing.JTextField();
        passwordLoginTextField = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JButton();
        forgotPasswordLabel = new javax.swing.JLabel();
        passwordIncorrectLabel = new javax.swing.JLabel();
        forgotPassPanel = new javax.swing.JPanel();
        securityQuestionComboBox = new javax.swing.JComboBox();
        securityQuestionLabel = new javax.swing.JLabel();
        userIdLabel = new javax.swing.JLabel();
        userIdTextField = new javax.swing.JTextField();
        answerLabel = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        submitButton = new javax.swing.JButton();
        passwordLabel = new javax.swing.JLabel();
        returnButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/logo.gif"))); // NOI18N

        mainPanel.setLayout(new java.awt.CardLayout());

        loginPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        userIdLoginLabel.setText("User Id : ");

        passwordLoginLabel.setText("Password : ");

        loginButton.setText("Login");
        loginButton.setName(LOGIN_BUTTON.getValue());
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });
        loginButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                loginButtonKeyPressed(evt);
            }
        });

        forgotPasswordLabel.setForeground(new java.awt.Color(51, 51, 255));
        forgotPasswordLabel.setText("<html><u>Forgot Password</u></html>");
        forgotPasswordLabel.setName(FORGOT_PASS_LABEL.getValue());
        forgotPasswordLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                forgotPasswordLabelMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                forgotPasswordLabelMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                forgotPasswordLabelMouseExited(evt);
            }
        });

        javax.swing.GroupLayout loginPanelLayout = new javax.swing.GroupLayout(loginPanel);
        loginPanel.setLayout(loginPanelLayout);
        loginPanelLayout.setHorizontalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginPanelLayout.createSequentialGroup()
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(passwordLoginLabel)
                            .addComponent(userIdLoginLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(loginPanelLayout.createSequentialGroup()
                                .addComponent(forgotPasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 160, Short.MAX_VALUE)
                                .addComponent(loginButton))
                            .addComponent(passwordLoginTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 298, Short.MAX_VALUE)
                            .addComponent(userIdLoginTextField)))
                    .addComponent(passwordIncorrectLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        loginPanelLayout.setVerticalGroup(
            loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userIdLoginLabel)
                    .addComponent(userIdLoginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLoginLabel)
                    .addComponent(passwordLoginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(loginPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(forgotPasswordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(loginButton))
                .addGap(18, 18, 18)
                .addComponent(passwordIncorrectLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(81, Short.MAX_VALUE))
        );

        mainPanel.add(loginPanel, "loginForm");

        forgotPassPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));

        securityQuestionLabel.setText("Security Question : ");

        userIdLabel.setText("User Id : ");

        answerLabel.setText("Answer : ");

        submitButton.setText("Submit");
        submitButton.setName(FORGOT_PASS_BUTTON.getValue());
        submitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });
        submitButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                submitButtonKeyPressed(evt);
            }
        });

        returnButton.setText("Return");
        returnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                returnButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout forgotPassPanelLayout = new javax.swing.GroupLayout(forgotPassPanel);
        forgotPassPanel.setLayout(forgotPassPanelLayout);
        forgotPassPanelLayout.setHorizontalGroup(
            forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(forgotPassPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(passwordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(forgotPassPanelLayout.createSequentialGroup()
                        .addComponent(submitButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(returnButton))
                    .addGroup(forgotPassPanelLayout.createSequentialGroup()
                        .addGroup(forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(securityQuestionLabel)
                            .addComponent(userIdLabel)
                            .addComponent(answerLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(securityQuestionComboBox, 0, 207, Short.MAX_VALUE)
                            .addComponent(userIdTextField)
                            .addComponent(passwordField))))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        forgotPassPanelLayout.setVerticalGroup(
            forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(forgotPassPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userIdLabel)
                    .addComponent(userIdTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(securityQuestionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(securityQuestionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(answerLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(forgotPassPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(submitButton)
                    .addComponent(returnButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passwordLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 61, Short.MAX_VALUE)
                .addContainerGap())
        );

        mainPanel.add(forgotPassPanel, "forgotPassForm");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 384, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void forgotPasswordLabelMouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_forgotPasswordLabelMouseClicked
		CardLayout cl = (CardLayout) (mainPanel.getLayout());
		cl.show(mainPanel, "forgotPassForm");
		controller.handleEvent(evt);
	}// GEN-LAST:event_forgotPasswordLabelMouseClicked

    private void forgotPasswordLabelMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_forgotPasswordLabelMouseEntered
    	forgotPasswordLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }//GEN-LAST:event_forgotPasswordLabelMouseEntered

    private void forgotPasswordLabelMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_forgotPasswordLabelMouseExited
    	forgotPasswordLabel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_forgotPasswordLabelMouseExited

	private void returnButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_returnButtonActionPerformed
		CardLayout cl = (CardLayout) (mainPanel.getLayout());
		cl.show(mainPanel, "loginForm");
	}// GEN-LAST:event_returnButtonActionPerformed

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitButtonActionPerformed
    	setSuccess(true);
    	//controller.handleEvent(evt);
    }//GEN-LAST:event_submitButtonActionPerformed

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
    	controller.handleEvent(evt);
    }//GEN-LAST:event_loginButtonActionPerformed

    private void loginButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loginButtonKeyPressed
    	controller.handleEvent(evt);
    }//GEN-LAST:event_loginButtonKeyPressed

    private void submitButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_submitButtonKeyPressed
    	controller.handleEvent(evt);
    }//GEN-LAST:event_submitButtonKeyPressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel answerLabel;
    private javax.swing.JPanel forgotPassPanel;
    private javax.swing.JLabel forgotPasswordLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton loginButton;
    private javax.swing.JPanel loginPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordIncorrectLabel;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel passwordLoginLabel;
    private javax.swing.JPasswordField passwordLoginTextField;
    private javax.swing.JButton returnButton;
    private javax.swing.JComboBox securityQuestionComboBox;
    private javax.swing.JLabel securityQuestionLabel;
    private javax.swing.JButton submitButton;
    private javax.swing.JLabel userIdLabel;
    private javax.swing.JLabel userIdLoginLabel;
    private javax.swing.JTextField userIdLoginTextField;
    private javax.swing.JTextField userIdTextField;
    // End of variables declaration//GEN-END:variables

	public javax.swing.JTextField getUserIdTextField() {
		return userIdTextField;
	}

	public javax.swing.JPasswordField getPasswordField() {
		return passwordField;
	}

	public javax.swing.JComboBox getSecurityQuestionComboBox() {
		return securityQuestionComboBox;
	}

	public javax.swing.JPasswordField getPasswordLoginTextField() {
		return passwordLoginTextField;
	}

	public javax.swing.JTextField getUserIdLoginTextField() {
		return userIdLoginTextField;
	}

	public void showPassword(String password) {
		passwordLabel.setText(password);
	}

	public void setSecurityQuestions(List<SecurityQuestion> securityQuestions) {
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		for(SecurityQuestion question : securityQuestions) {
			model.addElement(question.getQuestion());
		}
		securityQuestionComboBox.setModel(model);
	}

	public void setSuccess(boolean success) {
//		if (!success) {
//			passwordIncorrectLabel.setText("User Id/Password incorrect");
//			attempt++;
//		} else {
		if(userIdLoginTextField.getText().equals("Testing") && passwordLoginTextField.getText().equals("Testing")) {
			this.dispose();
			RealEstateUtilityController controller = new RealEstateUtilityController();
			JFrame frame = (JFrame) controller.getView();
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.pack();
			frame.setVisible(true);
			frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
			return;
		} else {
			passwordIncorrectLabel.setText("User Id/Password incorrect");
		}
//		}
//		if (attempt.equals(10)) {
//			System.exit(0);
//		}

	}
}
